package com.benjaminwan.read.free.base.activity

import android.content.res.Configuration
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.benjaminwan.read.free.app.App
import com.benjaminwan.read.free.common.SP_KEEP_SCREEN_ON
import com.benjaminwan.read.free.common.appPrefs
import com.benjaminwan.read.free.utils.RotationMode
import com.benjaminwan.read.free.utils.ScreenRotationObserver
import com.benjaminwan.read.free.utils.moshiAny
import com.benjaminwan.read.free.utils.setKeepScreenOnEnable
import com.xuexiang.xui.XUI
import me.jessyan.autosize.internal.CustomAdapt

abstract class BaseActivity : AppCompatActivity(), CustomAdapt {

    private var spKeepScreenOn: Boolean by App.INSTANCE.appPrefs.moshiAny(
        SP_KEEP_SCREEN_ON, true
    )

    private val screenRotationObserver: ScreenRotationObserver by lazy {
        ScreenRotationObserver(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        XUI.initTheme(this)
        super.onCreate(savedInstanceState)
        this.setKeepScreenOnEnable(spKeepScreenOn)//保持屏幕常亮
    }

    override fun isBaseOnWidth(): Boolean {
        return resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT
    }

    override fun getSizeInDp(): Float {
        return 360F
    }

    override fun onStart() {
        super.onStart()
        screenRotationObserver.startObserver()
    }

    override fun onStop() {
        screenRotationObserver.stopObserver()
        super.onStop()
    }

    fun enableRotation() {
        screenRotationObserver.enable = true
    }

    fun disableRotation() {
        screenRotationObserver.enable = false
    }

    fun setRotationMode(mode: RotationMode = RotationMode.ALL) {
        screenRotationObserver.setRotationMode(mode)
    }

}